package com.mycomp.cm.dao.impl;

import java.util.Set;

import javax.persistence.PersistenceContext;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mycomp.cm.dao.UserDAO;
import com.mycomp.cm.model.Contact;
import com.mycomp.cm.model.User;

@Repository // DB Layer specific Ex are Mapped to Spring specific Ex
@Transactional
public class UserDAOImpl implements UserDAO {

	private SessionFactory sessionFactory;

	// @PersistenceContext
	// private HibernateTemplate hibernateTemplate;

	@Autowired
	public UserDAOImpl(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void save(User user) {
		sessionFactory.getCurrentSession().save(user);
		// hibernateTemplate.save(user);
	}

	
	public User findByID(String userName) {
		return (User) sessionFactory.getCurrentSession().get(User.class,
				userName);

	}


	public Set<Contact> findUserContacts(String userName) {
		User user = (User) sessionFactory.getCurrentSession().get(User.class,
				userName);
		return user.getUserContacts();
	}

}
